#include <project.h>
/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module PDT.c - Product Definition Table maintenance functions |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Config/bdt.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#define VSPDT_C

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <aclstr.h>
#include <ctype.h>

#ifdef FLEET_DEFINED

#ifdef DEBUG_VSPDT
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <pdt.h>
#include <gds.h>
#include <cdt.h>
#include <hdt.h>
#include <confio.h>
#include <msg.h>
#include <msgfuncs.h>
#include <fields.h>
#include <util.h>
#include <spdebug.h>



typedef struct TagPDT_REC {
    char szProductCode[PDT_PROD_CODE_SIZE + 1];                      /* Product Code */
    char szProductDesc[PDT_DESC_SIZE + 1]; /* Product Description */
    char szPrice[PDT_AMT_SIZE + 1];      /* Product Unit Price */
} PDT_REC;


static PDT_REC srPDTRec;


int inLoadPDTRec(int inPDTRecNo) {
    if (inLoadConfRec(PDT_FILE_NAME, SIZE_PDT_REC, inPDTRecNo, (char *) &srPDTRec) != VS_SUCCESS)
        return(VS_ERR);

    return (VS_SUCCESS);
}

int inSavePDTRec(int inPDTRecNo) {
    if (inSaveConfRec(PDT_FILE_NAME, SIZE_PDT_REC, inPDTRecNo, (char *) &srPDTRec) != VS_SUCCESS)
        return(VS_ERR);

    return (VS_SUCCESS);
}


char* szGetProductCode(void) {
    return(srPDTRec.szProductCode);
}

char* szGetProductDescription(void) {
    return(srPDTRec.szProductDesc);
}

char* szGetProductPrice(void) {
    return(srPDTRec.szPrice);
}

void vdSetProductCode(char* szCode) {
    strlcpy(srPDTRec.szProductCode, szCode, sizeof(srPDTRec.szProductCode));
}

void vdSetProductDescription(char* szDesc) {
    strlcpy(srPDTRec.szProductDesc, szDesc, sizeof(srPDTRec.szProductDesc));
}

void vdSetProductPrice(char* szPrice) {
    strlcpy(srPDTRec.szPrice, szPrice, sizeof(srPDTRec.szPrice));
}

#define PDT_RECORD_SIZE 30

int inGetProductTable() {
    int x,
    i,
    j;
    int inFileHandle;
    char szKey[4];
    char szBuffer[PDT_RECORD_SIZE + 1];
    char szNum[PDT_PROD_CODE_SIZE + 1];


    inFileHandle = open(PDT_FILE_NAME, O_RDWR | O_CREAT);
    close(inFileHandle);

    x = 1;

    while (1) {
        sprintf(szKey, "PDT%02i", x);

        memset(szBuffer, 0x00, sizeof(szBuffer));

        if (get_env(szKey, szBuffer, PDT_RECORD_SIZE) > 0) {
            i = 0;
            while (szBuffer[i] != ':' && szBuffer[i] != '.' && i < PDT_PROD_CODE_SIZE) {
                szNum[i] = szBuffer[i];
                i++;
            }

            if (i == 1) {
                szNum[1] = szNum[0];
                szNum[0] = '0';
                szNum[2] = 0x00;
            } else
                szNum[i] = 0x00;                
            vdSetProductCode(szNum); 

            j = 0;
            i++;
            while (szBuffer[i] != ':' && szBuffer[i] != '.' && j < PDT_DESC_SIZE) {
                srPDTRec.szProductDesc[j] = szBuffer[i];
                i++;
                j++;
            }
            srPDTRec.szProductDesc[j] = 0x00;

            i++;
            j = 0;
            while (szBuffer[i] != 0x00 && j < PDT_AMT_SIZE) {
                srPDTRec.szPrice[j] = szBuffer[i];
                i++;
                j++;
            }
            srPDTRec.szPrice[j] = 0x00;

            inSavePDTRec(x - 1);

            put_env(szKey, "", 0);
        } else
            break;


        x++;
    }

    return(VS_SUCCESS);
}


VS_BOOL fValidProductCode(char* szCode) {
    if ((isdigit(szCode[0]) || isalpha(szCode[0])) && (isdigit(szCode[1]) || isalpha(szCode[1])))
        return(VS_TRUE);
    else
        return(VS_FALSE);
}


#endif

#undef VSPDT_C

